/**
 * 
 */
package com.java.test.messagecenter.mapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.messagecenter.dto.TerminalInfo;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Component
public class TerminalInfoMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectterminalCount(TerminalInfo info) {

		String sql = "select count(*) from terminal_terminalinfo where 1=1 ";

		if (info.getTerminalName() != null && !"".equals(info.getTerminalName())) {
			sql += " and terminal_name ='" + info.getTerminalName() + "'";
		}

		if (info.getAddTime() != null) {
			sql += " and TO_DAYS(add_time)= TO_DAYS('" + info.getAddTime() + "')";
		}

		/*
		 * if (info.getTchannelId() != null && !"".equals(info.getTchannelId())) { sql
		 * += " and tchannel_id like '%" + info.getTchannelId() + "%'"; }
		 */
	
		int i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i;
	}

	public List<TerminalInfo> selectterminalList(TerminalInfo info, PageInfo<TerminalInfo> page) {

		String sql = "select * from terminal_terminalinfo where 1=1 ";

		if (info.getTerminalName() != null && !"".equals(info.getTerminalName())) {
			sql += " and terminal_name ='" + info.getTerminalName() + "'";
		}

		if (info.getAddTime() != null) {
			sql += " and TO_DAYS(add_time)= TO_DAYS('" + info.getAddTime() + "')";
		}

		if (info.getTchannelId() != null && !"".equals(info.getTchannelId())) {
			sql += " and tchannel_id like '%" + info.getTchannelId() + "%'";
		}

		/*
		 * int start = (page.getCurrentPage() - 1) * page.getPageCount(); int limit =
		 * page.getPageCount();
		 * 
		 * sql += " limit ?,?";
		 */
		sql+=" order by add_time desc";
		List<TerminalInfo> list = jdbcTemplate.query(sql, new TerminalInfo());

		return list;
	}

	public List<TerminalInfo> selectallchannelByterminalnName(String terminalname){	
		String sql = "select * from terminal_terminalinfo where terminal_name=?";
		
		List<TerminalInfo> list = jdbcTemplate.query(sql, new TerminalInfo(),terminalname);
		
		return list;
	}
	
	
	
	public TerminalInfo selectterminalbyId(TerminalInfo info) {

		String sql = "select * from terminal_terminalinfo where id=?";

		TerminalInfo obj = jdbcTemplate.queryForObject(sql, new TerminalInfo(), info.getId());

		return obj;
	}

	public TerminalInfo selectterminalbychannelId(String channelId) {

		String sql = "select * from terminal_terminalinfo where tchannel_id=? ";

		List<TerminalInfo> list = jdbcTemplate.query(sql, new TerminalInfo(), channelId);

		if (list == null || list.size() <= 0) {
			return null;
		}

		return list.get(0);
	}

	public int insertterminalInfo(TerminalInfo info) {

		String sql = "insert into terminal_terminalinfo(add_people,add_time,area,content,tchannel_id,terminal_name,tchannel_name)values"
				+ "(?,?,?,?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getAddPeople(), info.getAddTime(), info.getAreaId(), info.getContent(),
				info.getTchannelId(), info.getTerminalName(),info.getTchannelName());

		return i;
	}

	public int updateterminalInfo(TerminalInfo info) {

		String sql = "update terminal_terminalinfo set area=?,content=?,tchannel_id=?,terminal_name=?,tchannel_name=? where id=?";

		int i = jdbcTemplate.update(sql, info.getAreaId(), info.getContent(), info.getTchannelId(),
				info.getTerminalName(), info.getTchannelName(),info.getId());

		return i;

	}

	public int deleteminalInfo(TerminalInfo info) {

		String sql = "delete from  terminal_terminalinfo  where id=?";

		int i = jdbcTemplate.update(sql, info.getId());

		return i;

	}

}
